home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / perlcl16.lha / perlclass1.6 / perltest.c++ < prev    next >
C/C++ Source or Header  |  1992-11-10  |  12KB  |  352 lines

  1. #ifdef    TEST
  2.  
  3. #include <iostream.h>
  4. /*
  5.  * V1.6
  6.  */
  7.  
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <stdio.h>
  11.  
  12. #ifdef    __TURBOC__
  13. #pragma hdrstop
  14. #endif
  15.  
  16. #include "perlclass.h"
  17.  
  18. int main()
  19. {
  20. PerlList<int> il, il1, il2, il3;
  21. PerlStringList x, y, z;
  22. int n;
  23.  
  24.     if(x) cout << "x is not empty" << endl;
  25.     else cout << "x is empty" << endl;
  26.  
  27.     if(x.isempty()) cout << "x.isempty() is true" << endl;
  28.     else cout << "x.isempty() is false" << endl;
  29.  
  30.     n= x.split("a b c d e f");
  31.  
  32.     if(x) cout << "x is not empty" << endl;
  33.     else cout << "x is empty" << endl;
  34.  
  35.     if(x.isempty()) cout << "x.isempty() is true" << endl;
  36.     else cout << "x.isempty() is false" << endl;
  37.  
  38.     cout << "x.split(a b c d e f)= " << n << ": " << x << endl;
  39.     cout << "x[0] = " << x[0] << endl;
  40.     z= x; z[0]= "x";
  41.     cout << "z= x; z[0]=\"x\" " << "z: " << z << endl;
  42.  
  43.     PerlString ss("1.2.3.4.5.6.7.8.9.0");
  44.     y= ss.split("\\.");
  45.     cout << "ss= " << ss << ", y= ss.split(\"\\.\"), y=" << endl << y << endl;
  46.     cout << "y.join(\" \")" << y.join(" ") << endl;       
  47.     {
  48.     PerlString xx= "a b c\nd e\tf   g";
  49.         cout << xx << endl << "xx.split()= " << xx.split() << endl;
  50.     xx= "a b c d e f g";
  51.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  52.     xx= "  a b c d e f g hi  ";
  53.     cout << xx << endl << "xx.split(\"\")= " << xx.split("") << endl;
  54.     xx= "a,b,c,d,,e,f,g,,,,";
  55.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  56.     xx= "a,b,c,d,,e,f,g,,";
  57.     cout << xx << endl << "xx.split(\",\", 5)= " << xx.split(",", 5) << endl;
  58.     xx= " a b c d e f g  ";
  59.     cout << xx << endl << "xx.split(\" \")= " << xx.split(" ") << endl;
  60.     xx= "a b c d,e,f g";
  61.     cout << xx << endl << "xx.split(\"([ ,])+\")= " << xx.split("([ ,])+") << endl;
  62.     xx= ",,,,";
  63.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  64.     xx= "";
  65.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  66.      xx= "   a b c\td    e\nf  g   ";
  67.     cout << xx << endl << "xx.split(\"' '\")= " << xx.split("' '") << endl;
  68.    }
  69.  
  70.     cout << "x = " << x << endl;
  71.  
  72.     cout << "x.pop() : " << x.pop() << ", " ;
  73.     cout << x.pop() << endl << "x= " << x << endl;;
  74.     cout << "x.shift() : " << x.shift() << ", ";
  75.     cout << x.shift() << endl<< "x= " << x << endl;
  76.     
  77.     x.unshift(y);
  78.     cout << "x.unshift(y): " << x << endl;
  79.     
  80.     if(il) cout << "il is not empty" << endl;
  81.     else cout << "il is empty" << endl;
  82.     
  83.     il.push(1); il.push(2); il.push(3); il.push(4);
  84.  
  85.     if(il) cout << "il is not empty" << endl;
  86.     else cout << "il is empty" << endl;
  87.     
  88.     cout << "il(1, 2, 3, 4) : " << il << endl;
  89.     il3= il; il3[0]= 9999;
  90.     cout << "il3= il; il3[0]= 9999; il3 = " << il3 << endl << "il= " << il << endl;
  91.  
  92.     il1= il.reverse();
  93.     cout << "il.reverse: " << il1 << endl;
  94.  
  95.     cout << "il1.sort(): " << il1.sort() << endl;
  96.  
  97.     y.reset();
  98.     y.push("one"); y.push("two"); y.push("three"); y.push("four");
  99.     cout << "y = " << endl << y;
  100.     cout << "y.reverse() " << y.reverse() << endl;
  101.     cout << "y.sort() " << y.sort() << endl;
  102.     cout << "y.sort().reverse() " << y.sort().reverse() << endl;
  103.         
  104.     il2.push(3); il2.push(4);
  105.     cout << "il2.push(3, 4) : " << il2 << endl;
  106.  
  107.     il.push(il2);    
  108.     cout << "il.push(il2) : " << il << endl;
  109.     
  110.     cout << "il.pop() : " << il.pop() << ", ";
  111.     cout << il.pop() << endl;
  112.     
  113.     il.unshift(il2);    
  114.     cout << "il.unshift(il2) : " << il << endl;
  115.     
  116.     cout << "il.shift() : " << il.shift() << ", ";
  117.     cout << il.shift() << endl;
  118.      
  119.     cout << "testing splice:" << endl;
  120.     x.reset();
  121.     x.split("a b c d e f g h i");
  122.     cout << "x = " << x << endl;
  123.     z= x.splice(2, 3);
  124.     cout << "z= x.splice(2, 3): z= " << z << endl << "x = " << x << endl;
  125.     cout << "x.splice(2, 0, z): " << x.splice(2, 0, z);
  126.     cout << "x= " << x << endl;
  127.     cout << "z.splice(1, 1, x): " << z.splice(1, 1, x);
  128.     cout << "z= " << z << endl;
  129.     cout << "x= " << x << endl;
  130.     cout << "z.splice(20, 1, x): " << z.splice(20, 1, x);
  131.     cout << "z= " << z << endl;
  132.     
  133.  // test auto expand
  134.     PerlList<int> ile;
  135.     ile[3]= 3;
  136.     cout << ile.scalar() << ", " << ile[3] << endl;
  137.     ile[100]= 1234;
  138.     ile[0]= 5678;
  139.     cout << ile.scalar() << ", " << ile[0] << ", " << ile[100] << endl;
  140.     PerlList<int> ile2;
  141.     for(int i=0;i<=100;i++) ile2[i]= i;
  142.     for(i=200;i>100;i--) ile2[i]= i;
  143.     for(i=0;i<=200;i++) if(ile2[i] != i) cout << "error at index " << i << endl;
  144.     cout << "Index check done" << endl;
  145.     cout << ile2.scalar() << ", " << ile2[0] << ", " << ile2[200] << endl;
  146.  
  147. // test Regexp stuff
  148.     cout << endl << "testing regexp stuff:" << endl;
  149.     x.reset();
  150.     cout << "x.m(\".*X((...)...(...))\", \"12345Xabcxyzdef\") returns " <<
  151.      x.m(".*X((...)...(...))", "12345Xabcxyzdef") << endl;
  152.     cout << "subs matched = " << x << endl;
  153.  
  154.     Regexp rexp("abc");
  155.     PerlString rst("12345Xabcxyzdef");
  156.     cout << "rst.m(rexp) returns " << rst.m(rexp) << endl;
  157.     
  158.     cout << endl << "testing grep:" << endl;
  159.     x.reset();
  160.     x.push("abcd"); x.push("a2345"); x.push("X2345"); x.push("Xaaaaa"); x.push("aaaaa");
  161.     
  162.     y= x.grep("^a.*");
  163.     cout << "x: " << endl << x << endl << "grep(^a.*)" << endl;
  164.     cout << "Expect 3 matches:" << endl << y << endl;
  165.     {
  166.     PerlString s1("abcdef");
  167.         cout << "s1= " << s1 << ", s1.m(\"^cde\") : " << s1.m("^cde") << endl;
  168.     cout << "s1= " << s1 << ", s1.m(\"^..cde\") : " << s1.m("^..cde") << endl;
  169.     }
  170.     {
  171.     PerlStringList sl;
  172.     PerlString str= "ab cd ef";
  173.     sl = m("(..) (..)", str);
  174.     cout << "sl = m(\"(..) (..)\", \"ab cd ef\"); sl = " << endl <<
  175.          sl << endl;
  176.     }
  177.  
  178.     {
  179.     Regexp ncr("abc", Regexp::nocase);
  180.     Regexp cr("abc");
  181.     PerlString s= "ABC";
  182.     cout << "s= " << s << ": s.m(ncr)= " << s.m(ncr) << endl;
  183.     cout << "s= " << s << ": s.m(cr)= " << s.m(cr) << endl;
  184.     cout << "s.m(\"abc\", \"i\")= " << s.m("abc", "i") << endl;
  185.     cout << "s.m(\"abc\")= " << s.m("abc") << endl;
  186.     }
  187.     
  188. // Test strings
  189.     cout << "test string stuff:" << endl;
  190.  
  191.     PerlString s1("string1"), s2, s3;
  192.     const char *s= s1;
  193.  
  194.     cout << "Empty string: " << s2 << " length= " << s2.length()
  195.      << ",  strlen(s2) = " << strlen(s2) << endl;
  196.     
  197.     cout << "s1:" << s1 << endl;
  198.     cout << "s[0]= " << s[0] << ", s[5]= " << s[5] << endl;
  199. //    s[2]= 'X';
  200. //    cout << "s[2]= 'X', s= " << s << endl;
  201. //    s[2]= 'r';
  202.     
  203.     cout << "const char *s= s1: s= " << s << endl;
  204.     s2= s1;
  205.     cout << "s2=s1,  s2:" << s2 << endl;
  206.     s1.chop();
  207.     cout << "s1.chop()" << s1 << endl;
  208.     s3= s;
  209.     cout << "s3= s: s3 = " << s3 << endl;
  210.     cout << "index(\"ri\") in " << s1 << ": " << s1.index("ri") << endl;
  211.     s3= "1";
  212.     cout << "index(" << s3 << ") in " << s1 << ": " << s1.index(s3) << endl;
  213.     s3= "abcabcabc";
  214.     cout << "rindex(abc) in" << s3 << ": " << s3.rindex("abc") << endl;
  215.     cout << "rindex(abc, 5) in" << s3 << ": " << s3.rindex("abc", 5) << endl;
  216.  
  217. // test substrings
  218.     cout << "substr(5, 3) in " << s3 << ": " << s3.substr(5, 3) << endl;
  219.     s3.substr(5, 3)= "XXX";
  220.     cout << "s3.substr(5, 3) = \"XXX\"" << s3 << endl;
  221.     s3.substr(5, 3)= s1;
  222.     cout << "s3.substr(5, 3) = s1" << s3 << endl;
  223.     s3.substr(5, 3)= s1.substr(1, 3);
  224.     cout << "s3.substr(5, 3) = s1.substr(1, 3)" << s3 << endl;
  225.     s3.substr(0, 6)= s1.substr(0, 3);
  226.     cout << "s3.substr(0, 6) = s1.substr(0, 3)" << s3 << endl;
  227.     s3.substr(-3, 2)= s1.substr(0, 2);
  228.     cout << "s3.substr(-3, 2) = s1.substr(0, 2)" << s3 << endl;
  229.  
  230. // test overlapping substrings
  231.     s1= "1234567890";
  232.     cout << "s1 = " << s1 << endl;
  233.     s1.substr(0, 10)= s1.substr(1, 9);
  234.     cout << "s1.substr(0, 10)= s1.substr(1, 9) " << s1 << endl;
  235.     s1= "1234567890";
  236.     cout << "s1 = " << s1 << endl;
  237.     s1.substr(1, 9)= s1.substr(0, 10);
  238.     cout << "s1.substr(1, 9)= s1.substr(0, 10) " << s1 << endl;
  239.  
  240.     // test over-large substrings
  241.     s1= "1234567890"; s1.substr(7, 10)= "abcdefghij";
  242.     cout << "s1.substr(7, 10)= \"abcdefghij\" " << s1 << endl;
  243.     s1= "1234567890"; s1.substr(10, 5)= "abcdefghij";
  244.     cout << "s1.substr(10, 5)= \"abcdefghij\" " << s1 << endl;
  245.     s1= "1234567890"; s1.substr(20, 1)= "abcdefghij";
  246.     cout << "s1.substr(20, 1)= \"abcdefghij\" " << s1 << endl;
  247.  
  248.     s1= "abcdef"; s2= "123456";
  249.      
  250.     cout << s1 << " + " << s2 << ": " << s1 + s2 << endl;
  251.     cout << s1 << " + " << "\"hello\"= " << s1 + "hello" << endl;
  252.     cout << "\"hello\"" << " + " << s1 << "= " << "hello" + s1 << endl;
  253.     cout << s1 << " + \'x\' = " << s1 + 'x' << endl;
  254.     
  255.     s1= "abc"; s2= "def"; s3= "abc";
  256.     cout << s1 << " == " << s2 << ": " << (s1 == s2) << endl; 
  257.     cout << s1 << " != " << s2 << ": " << (s1 != s2) << endl;
  258.     cout << s1 << " == " << s3 << ": " << (s1 == s3) << endl; 
  259.     cout << s1 << " != " << s3 << ": " << (s1 != s3) << endl;
  260.     cout << s1 << " < " << s2 << ": " << (s1 < s2) << endl; 
  261.     cout << s1 << " > " << s2 << ": " << (s1 > s2) << endl; 
  262.     cout << s1 << " <= " << s2 << ": " << (s1 <= s2) << endl; 
  263.     cout << s1 << " >= " << s3 << ": " << (s1 >= s3) << endl; 
  264.  
  265. // Test the tr() functions
  266.     s1= "abcdefghi";
  267.     cout << "s1 = " << s1;
  268.     cout << ", s1.tr(\"ceg\", \"123\") = " << s1.tr("ceg", "123");
  269.     cout << ", s1 = " << s1 << endl;
  270.     s1= "abcdefghi";
  271.     cout << "s1.tr(\"a-z\", \"A-Z\") = " << s1.tr("a-z", "A-Z");
  272.     cout << ", s1 = " << s1 << endl;
  273.     s1= "abcdefghi";
  274.     cout << "s1.tr(\"efg\", \"\") = " << s1.tr("efg", "");
  275.     cout << ", s1 = " << s1 << endl;
  276.     s1= "abcdefghi";
  277.     cout << "s1.tr(\"ac-e\", \"X\") = " << s1.tr("ac-e", "X");
  278.     cout << ", s1 = " << s1 << endl;
  279.     s1= "abcdefghiiii";
  280.     cout << "s1 = " << s1;
  281.     cout << ", s1.tr(\"ac-e\", \"X\", \"s\") = " << s1.tr("ac-e", "X", "s");
  282.     cout << ", s1 = " << s1 << endl;
  283.     s1= "abcdefghi";
  284.     cout << "s1.tr(\"ac-e\", \"\", \"d\") = " << s1.tr("ac-e", "", "d");
  285.     cout << ", s1 = " << s1 << endl;
  286.     s1= "abcdefghi";
  287.     cout << "s1.tr(\"ac-e\", \"d\", \"d\") = " << s1.tr("ac-e", "d", "d");
  288.     cout << ", s1 = " << s1 << endl;
  289.     s1= "abcdefghi";
  290.     cout << "s1.tr(\"ac-e\", \"\", \"cd\") = " << s1.tr("ac-e", "", "cd");
  291.     cout << ", s1 = " << s1 << endl;
  292.     s1= "bookkeeper";
  293.     cout << s1;
  294.     cout << ": s1.tr(\"a-zA-Z\", \"\", \"s\") = " << s1.tr("a-zA-Z", "", "s");
  295.     cout << ", s1 = " << s1 << endl;
  296.     s1= "abc123def456ghi";
  297.     cout << s1;
  298.     cout << ": s1.tr(\"a-zA-Z\", \" \", \"c\") = " << s1.tr("a-zA-Z", " ", "c");
  299.     cout << ", s1 = " << s1 << endl;
  300.     s1= "abc123def456ghi789aaa";
  301.     cout << s1;
  302.     cout << ": s1.tr(\"a-zA-Z\", \" \", \"cs\") = " << s1.tr("a-zA-Z", " ", "cs");
  303.     cout << ", s1 = " << s1 << endl;
  304.     s1= "abcdddaaaxxx";
  305.     cout << s1;
  306.     cout << ": s1.tr(\"a\", \"d\", \"s\") = " << s1.tr("a", "d", "s");
  307.     cout << ", s1 = " << s1 << endl;
  308.     
  309. // Test substitute command
  310.     s1= "abcdefghi";
  311.     cout << s1;
  312.     cout <<" s1.s(\"def\", \"FED\") = " << s1.s("def", "FED");
  313.     cout << ", s1= " << s1 << endl;
  314.     s1= "abcDEFghi";
  315.     cout << s1;
  316.     cout <<" s1.s(\"def\", \"FED\") = " << s1.s("def", "FED");
  317.     cout << ", s1= " << s1 << endl;
  318.     s1= "abcDEFghi";
  319.     cout << s1;
  320.     cout <<" s1.s(\"def\", \"FED\", \"i\") = " << s1.s("def", "FED", "i");
  321.     cout << ", s1= " << s1 << endl;
  322.     s1= "abcdefghi";
  323.     cout << s1;
  324.     cout <<" s1.s(\"(...)(...)\", \"\\$,$&,$2 $1\") = " <<
  325.          s1.s("(...)(...)", "\\$,$&,$2 $1");
  326.     cout << ", s1= " << s1 << endl;
  327.     s1= "abcdefabcghiabc";
  328.     cout << s1;
  329.     cout <<" s1.s(\"abc\", \"XabcX\", \"g\") = " << s1.s("abc", "XabcX", "g");
  330.     cout << ", s1= " << s1 << endl;
  331.     s1= "abcdefabcghiabc";
  332.     cout << s1;
  333.     cout <<" s1.s(\"abc\", \"X\", \"g\") = " << s1.s("abc", "X", "g");
  334.     cout << ", s1= " << s1 << endl;
  335.     s1= "abcdefabcghiabc";
  336.     cout << s1;
  337.     cout <<" s1.s(\"abc(.)\", \"X$1abcX$1\", \"g\") = " <<
  338.          s1.s("abc(.)", "X$1abcX$1", "g");
  339.     cout << ", s1= " << s1 << endl;
  340.     s1= "abcdefabcghiabc";
  341.     cout << s1;
  342.     cout <<" s1.s(\"(.)abc\", \"$1X$1abcX\", \"g\") = " <<
  343.          s1.s("(.)abc", "$1X$1abcX", "g");
  344.     cout << ", s1= " << s1 << endl;
  345.     s1= "1234567890";
  346.     cout << s1;
  347.     cout <<" s1.s(\"(.)(.)\", \"$2$1\", \"g\") = " <<
  348.          s1.s("(.)(.)", "$2$1", "g");
  349.     cout << ", s1= " << s1 << endl;
  350. }
  351. #endif
  352.